package programmierung.aufgabe5;

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

@Author(value = { "Baeumann, Vinzent", "Maciuch, Miroslav" })
public class EwnGui extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 453335L;
	private JButton[] buttons;
	private int[][] buttonMap;

	public EwnGui(String[] field) {
		super("Einstein würfelt nicht");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new GridLayout(5, 5));
		setResizable(false);
		setSize(500, 500);

		buttons = new JButton[25];

		for (int i = 0; i < buttons.length; i++) {
			buttons[i] = new JButton(new ImageIcon(getClass().getResource(
					"img/spacer.png")));
			add(buttons[i]);
		}

		buttonMap = new int[5][5];
		int counter = 0;

		for (int i = 0; i < buttonMap.length; i++) {
			for (int j = 0; j < buttonMap[i].length; j++) {
				buttonMap[i][j] = counter;
				counter++;
			}
		}

		parseField(field);

		setVisible(true);
	}

	private void parseField(String[] field) {
		String[] split = null;
		String fieldName = null;

		for (int i = 0; i < field.length; i++) {
			split = field[i].split(" ");
			switch (split[0]) {
			case "RED":
				fieldName = "img/rot" + getNumber(split[1]) + ".png";
				setButton(fieldName, split[2], split[3]);
				break;
			case "BLUE":
				fieldName = "img/blau" + getNumber(split[1]) + ".png";
				setButton(fieldName, split[2], split[3]);
				break;
			}
		}
	}

	private void setButton(String fieldName, String x, String y) {
		buttons[buttonMap[5 - Integer.valueOf(y)][Integer.valueOf(x) - 1]]
				.setIcon(new ImageIcon(getClass().getResource(fieldName)));
	}

	private String getNumber(String number) {
		// TODO Auto-generated method stub
		switch (number) {
		case "ONE":
			return "1";
		case "TWO":
			return "2";
		case "THREE":
			return "3";
		case "FOUR":
			return "4";
		case "FIVE":
			return "5";
		case "SIX":
			return "6";
		default:
			return "0";
		}
	}
}
